package com.topvision.gsj.launcher;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


@SuppressLint("DrawAllocation")
public class BatteryView extends View{

	private int mPower = 100;
	Context context;
	TextView powerLevel;
	private  String powerNum = "";
	
	public BatteryView(Context context) {
		super(context);
		this.context = context;
	}

	public BatteryView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int containWith = getWidth();
		int containHeight = getHeight();
		int battery_left = 0;
		int battery_top = 0;
		int battery_width = 35;
		int battery_height = 15;
		
		int battery_head_width = 3;
		int battery_head_height = 3;
		
		int battery_inside_margin = 3;
		
		battery_top = containHeight/2 -battery_height/2;
		
		
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setAntiAlias(true);
		paint.setStyle(Style.STROKE);
		
		Rect rect = new Rect(battery_left, battery_top, 
				battery_left + battery_width, battery_top + battery_height);
	
		canvas.drawRect(rect, paint);
		//ֵ
		 Paint textPaint = new Paint();  
		 textPaint.setTextSize(18);  
		 textPaint.setColor( Color.WHITE);  
		 canvas.drawText(powerNum, battery_left+battery_width+5,battery_top+battery_height/2+7, textPaint);
		
		float power_percent = mPower / 100.0f;
		
		Paint paint2 = new Paint(paint);
		paint2.setStyle(Style.FILL);
		
		if(power_percent != 0) {
			int p_left = battery_left + battery_inside_margin;
			int p_top = battery_top + battery_inside_margin;
			int p_right = p_left - battery_inside_margin + (int)((battery_width - battery_inside_margin) * power_percent);
			int p_bottom = p_top + battery_height - battery_inside_margin * 2;
			Rect rect2 = new Rect(p_left, p_top, p_right , p_bottom);
			canvas.drawRect(rect2, paint2);
		}
		
		
		int h_left = battery_left + battery_width;
		int h_top = battery_top + battery_height / 2 - battery_head_height / 2;
		int h_right = h_left + battery_head_width;
		int h_bottom = h_top + battery_head_height;
		Rect rect3 = new Rect(h_left, h_top, h_right, h_bottom);
		canvas.drawRect(rect3, paint2);
		
	}
	
	public void setPower(int power) {
		mPower = power;
		if(mPower < 0) {
			mPower = 0;	
		}
		powerNum = mPower+"";
		invalidate();
	}
	
	
}
